#-*- coding: utf-8 -*-
#! /usr/bin/python

import time
import win32api
import win32con
import ConfigParser
import hashlib
import datetime


def initConfig(filename):
    '''
    去掉文件头BOM等
    '''
    import re
    content = open(filename).read()
    content = re.sub(r"\xfe\xff", "", content)
    content = re.sub(r"\xff\xfe", "", content)
    content = re.sub(r"\xef\xbb\xbf", "", content)
    open(filename, 'w').write(content)


def logit(content):
    '''
    另一种log记录函数
    '''
    file = open('error.txt', 'a')
    try:
        timestr = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')+': '
        file.write(timestr+content+'\r\n')
    finally:
        file.close()


def ordd(str):
    '''
    解决键盘码不对应问题,不知道别的电脑有没有这个问题
    '''
    t = ord(str)
    #65到90是A到Z
    if(t>90):
        return t-32
    else:
        return t


def pressMe():
    '''
    模拟人工摇
    '''
    win32api.keybd_event(37, 0, 0, 0)#left
    time.sleep(4)
    win32api.keybd_event(37, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(0.5)
    win32api.keybd_event(39, 0, 0, 0)#right
    time.sleep(1)
    win32api.keybd_event(39, 0, win32con.KEYEVENTF_KEYUP, 0)


def makesn(id):
    '''
    id是微信号，中英文都可以
    '''
    x = hashlib.sha224((hashlib.md5(id)).hexdigest()+'jhfaixiaobao')
    return x.hexdigest()


def mfunc():
    config = ConfigParser.ConfigParser()
    initConfig('shake.ini')
    config.read('shake.ini')
    sn = config.get("global", "sn")
    id = config.get("global", "id")
    wtime = config.getint("global", "time")
    if(not makesn(id)==sn):
        print 'SN Error!!'
        logit('SN Error!!')
        exit(0)
    else:
        pressMe()
        print 'ok'
        time.sleep(wtime)
    
import wx

def create(parent):
    return wxFrame1(parent)

[wxID_WXFRAME1, wxID_WXFRAME1BUTTON1, wxID_WXFRAME1BUTTON2, 
 wxID_WXFRAME1BUTTON3, 
] = [wx.NewId() for _init_ctrls in range(4)]

class wxFrame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
              pos=wx.Point(299, 227), size=wx.Size(370, 146),
              style=wx.DEFAULT_FRAME_STYLE, title='Shaking...')
        self.SetClientSize(wx.Size(362, 119))

        self.button1 = wx.Button(id=wxID_WXFRAME1BUTTON1, label='Start>>',
              name='button1', parent=self, pos=wx.Point(112, 16),
              size=wx.Size(152, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_WXFRAME1BUTTON1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        """ Example of a frame kept as a reference

        Note it was created in __init__
        """
            mfunc()



if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    # needed when running from Boa under Windows 9X
    frame.Show();frame.Hide();frame.Show()

    app.MainLoop()
